<?php
	class linkAction extends BaseAction{
		public function index(){
			$link = M('Link');
			import("ORG.Util.Page");
			$count = $link->count();
			$page = new Page($count,c('PAGESIZE'));
			$show = $page->show();
			$data = $link->limit($page->firstRow.','.$page->listRows)
							->field(true)
							->select();
			$this->assign("list",$data);
			$this->assign('page',$show);
			$this->display();
		}

		//通过审核
		public function pass(){
			if(empty($_GET['id'])){
				$this->error('请选择要审核的友情链接！');
				exit();
			}
			$link = M('link');
			$linkData = $link->getById($_GET['id']);
			if(!$linkData){
				$this->error('请确保要审核的友情链接ID正确！');
				exit();
			}
			$data = array();
			$data['state'] = 1;
			if($link->where('id = '.$_GET['id'])->save($data)){
				$this->success('审核成功！');
			}else{
				$this->error('审核失败！');
			}
		
		}
		
		//取消审核
		public function cancel(){
			if(empty($_GET['id'])){
				$this->error('请选择要取消审核的友情链接！');
				exit();
			}
			$link = M('link');
			$linkData = $link->getById($_GET['id']);
			if(!$linkData){
				$this->error('请确保要取消审核的友情链接ID正确！');
				exit();
			}
			$data = array();
			$data['state'] = 0;
			if($link->where('id = '.$_GET['id'])->save($data)){
				$this->success('取消审核成功！');
			}else{
				$this->error('取消审核失败！');
			}
		}
		
		public function add(){
			$this->display();
		}

		public function insert(){
			$link = D('Link');
			if($link->create())
			{
				if($link->add())
				{
					$this->assign("jumpUrl","__URL__");
					$this->success("添加成功！");
				}else{
					$this->error("添加失败！");
				}
			}else{
				$this->error($link->getError());
			}
		}

		public function edit()
		{
			if(!empty($_GET['id']))
			{
				$link = M('Link');
				$data = $link->getById($_GET['id']);
				// 				var_dump($data);
				if(null == $data)
					$this->error("请确认要修改的友情链接ID合法！");
				$this->assign('link',$data);
				$this->display();

			}else{
				$this->error("请选择要修改的友情链接！");
			}
		}

		public function update()
		{
			$link = D('Link');
			if($link->create()){
				if($link->save()){
					$this->success("更新成功！");
				}else{
					$this->error("因为数据没变化或更新失败！");
				}
			}else{
				$this->error($link->getError());
			}

		}

		public function delete()
		{
			if(!empty($_GET['id']))
			{
				$link = M('Link');
				if($link->delete($_GET['id'])){
					$this->success("删除友情链接成功!");
				}else{
					$this->error("删除友情链接失败!");
				}
			}else{
				$this->error("请选择要删除的友情链接！");
			}
		}
	}
?>